package net.lab1024.sa.admin.module.business.contact.service;

import java.util.List;
import net.lab1024.sa.admin.module.business.contact.dao.ContactDao;
import net.lab1024.sa.admin.module.business.contact.domain.entity.ContactEntity;
import net.lab1024.sa.admin.module.business.contact.domain.form.ContactAddForm;
import net.lab1024.sa.admin.module.business.contact.domain.form.ContactQueryForm;
import net.lab1024.sa.admin.module.business.contact.domain.form.ContactUpdateForm;
import net.lab1024.sa.admin.module.business.contact.domain.vo.ContactVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 联系方式 Service
 *
 * @Author 1
 * @Date 2025-06-20 16:36:02
 * @Copyright 1
 */

@Service
public class ContactService {

    @Resource
    private ContactDao contactDao;

    /**
     * 分页查询
     */
    public PageResult<ContactVO> queryPage(ContactQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ContactVO> list = contactDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(ContactAddForm addForm) {
        ContactEntity contactEntity = SmartBeanUtil.copy(addForm, ContactEntity.class);
        ContactQueryForm contact = new ContactQueryForm();
        contact.setPhone(addForm.getPhone());
        List<ContactVO> contactEntities = contactDao.list(contact);
        if (CollectionUtils.isNotEmpty(contactEntities)) {
            return ResponseDTO.userErrorParam("手机号已存在");
        } else {
            contactDao.insert(contactEntity);
        }
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(ContactUpdateForm updateForm) {
        ContactEntity contactEntity = SmartBeanUtil.copy(updateForm, ContactEntity.class);
        contactDao.updateById(contactEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        contactDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        contactDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public List<ContactVO> list(ContactQueryForm contact) {
        List<ContactVO> list = contactDao.list(contact);
        return list;
    }
}
